package com.darren.archive.dao.impl;

import com.darren.archive.dao.SysDepartmentDao;
import com.darren.archive.entity.SysDepartment;
import com.darren.archive.entity.SysFunction;
import com.darren.framework.dao.impl.GenericDaoImpl;
import com.darren.framework.utils.StringUtils;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class SysDepartmentDaoImpl extends GenericDaoImpl<SysDepartment, Long> implements SysDepartmentDao {

    /**
     * 根据部门名称，获取系统部门树型结构
     *
     * @param departmentName 系统菜单名称，用于模糊查询
     * @return
     */
    @Override
    public List<SysDepartment> getSysDepartmentByName(String departmentName) {
        Session session = sessionFactory.getCurrentSession();
        String hSQL = "select f from SysDepartment f  where  f.delete = false ";
        Query query = null;
        if (StringUtils.isEmpty(departmentName)) {
            query = session.createQuery(hSQL, SysDepartment.class);
            return query.list();
        } else {
            hSQL = "select f from SysDepartment f  where (f.shortName like :departmentName or f.fullName like :departmentName) and  f.delete = false ";
            query = session.createQuery(hSQL, SysDepartment.class);
            query.setParameter("departmentName", "%" + departmentName + "%");
            return query.list();
        }
    }
}